Menus in Tkinter (GUI Programming) 您所在的位置:网站首页 tkinter menu delete Menus in Tkinter (GUI Programming)

Menus in Tkinter (GUI Programming)

2024-06-30 06:08| 来源: 网络整理| 查看: 265

The tkinter menu is a top-level pulldown menu. They are shown just under the title bar, as you鈥檇 expect from traditional gui apps.

The menu can have multiple sub menus and each sub menu can contain items. Menu items can be associated with callback methods, meaning when you click them a Python method is called.

Related course: Python Desktop Apps with Tkinter

ExampleIntroduction

Adding a menu is very straightforward, but it can be a bit confusing if it鈥檚 the first time you鈥檙e doing it. First create the top menu with these lines:

123self.master = mastermenu = Menu(self.master)self.master.config(menu=menu)

Then you can add menus to this menu:

12345fileMenu = Menu(menu)menu.add_cascade(label="File", menu=fileMenu)editMenu = Menu(menu)menu.add_cascade(label="Edit", menu=editMenu)

Each of those sub menus can have items:

1234fileMenu.add_command(label="Item")fileMenu.add_command(label="Exit", command=self.exitProgram)editMenu.add_command(label="Undo")editMenu.add_command(label="Redo")

Menu items can be clickable, you can specify the callback method in the same way as buttons (command=). The click will then call a Python method.

tkinter menu

tkinter menu example

The menu example below adds a menu to a basic tkinter window. It has one clickable menu item but shows a complete menu.

123456789101112131415161718192021222324252627from tkinter import *class Window(Frame): def __init__(self, master=None): Frame.__init__(self, master) self.master = master menu = Menu(self.master) self.master.config(menu=menu) fileMenu = Menu(menu) fileMenu.add_command(label="Item") fileMenu.add_command(label="Exit", command=self.exitProgram) menu.add_cascade(label="File", menu=fileMenu) editMenu = Menu(menu) editMenu.add_command(label="Undo") editMenu.add_command(label="Redo") menu.add_cascade(label="Edit", menu=editMenu) def exitProgram(self): exit() root = Tk()app = Window(root)root.wm_title("Tkinter window")root.mainloop()

Download Tkinter Examples



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有